//
//  STFileItem.m
//  smart_transferor
//
//  Created by wtc on 11-11-2.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "STFileItem.h"
#import "MobileCoreServices/MobileCoreServices.h"
#import <QuickLook/QuickLook.h>
@implementation STFileItem

@synthesize fileName,fileType,icon,absPath,ic;


@interface UIImage (scale)  

-(UIImage*)scaleToSize:(CGSize)size;  

@end  




- (int) getFileTypeFromName:(NSString *)name
{
    NSString * uti = ic.UTI; 
    if(UTTypeConformsTo((CFStringRef)uti,CFSTR("public.image")))
        return kImage;
    else if(UTTypeConformsTo((CFStringRef)uti,CFSTR("public.movie"))) 
        return kVideo;
    else if(UTTypeConformsTo((CFStringRef)uti,CFSTR("public.audio")))
        return kAudio;
    else return kOther;
}

- (UIImage *) getThumbnailImageFromName:(NSString *)filename
{
   
    NSInteger iconCount = [ic.icons count];
    UIImage * img = [ic.icons objectAtIndex:iconCount - 1 ];
    //img = QLThumbnailImageCreate(nil,ic.URL,CGSizeMake(60, 60),0);
    if(!(self.fileType == kImage ||
       self.fileType == kVideo ||
       self.fileType == kAudio))
    {
        img = [ic.icons objectAtIndex:0 ];
    }
    
    img = [img scaleToSize:CGSizeMake(60, 60) ] ;
    return img;
}

- (STFileItem *) initWithFileName:(NSString*)filename AndDir:(NSString *)dir
{
    self.fileName = filename;
    absPath = [self composeAbsPathByDir:dir AndName:fileName];
    
    NSURL * url = [[NSURL alloc ]initFileURLWithPath:absPath isDirectory:NO];//[[NSURL alloc] initFileURLWithPath:absPath isDirectory:NO];
    ic = [UIDocumentInteractionController interactionControllerWithURL:url];
    ic.URL = url;
    [url release ];
    
    fileType = [self getFileTypeFromName:filename];
    
    
    
    self.icon = [self getThumbnailImageFromName:fileName];
    
    return self;
}

- (NSString *) composeAbsPathByDir:(NSString *) absDir AndName:(NSString *)filename
{
    self.absPath = [[absDir stringByAppendingString:@"/"] stringByAppendingString:fileName];
//    const char *fnameStr;
//    fnameStr =[absPath UTF8String];
    
    return absPath;
}

@end

@implementation UIImage (scale)  

-(UIImage*)scaleToSize:(CGSize)size  
{  
    // 创建一个bitmap的context  
    // 并把它设置成为当前正在使用的context  
    UIGraphicsBeginImageContext(size);  
    
    // 绘制改变大小的图片  
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];  
    
    // 从当前context中创建一个改变大小后的图片  
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
    
    // 使当前的context出堆栈  
    UIGraphicsEndImageContext();  
    
    // 返回新的改变大小后的图片  
    return scaledImage;  
}  

@end  